\documentclass[border=10pt,tikz]{standalone}
\usepackage{pgfplots,pgfplotstable,tikz}
\usetikzlibrary{intersections,matrix}
\usepackage{verbatim}
\usepackage{moresize}
\usetikzlibrary{calc}
\usepackage{datatool}
\usetikzlibrary{shapes}
\usetikzlibrary{plotmarks}
\usepackage{csvsimple}
\usepackage{siunitx}
\usepackage{mathtools}
\usepackage{amsmath,amssymb}
\usetikzlibrary{shapes.geometric}
\usepackage{booktabs}
\usepackage{xspace} 
\usepackage{etoolbox}
\usepackage{multirow}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.text}
\usetikzlibrary{decorations.pathmorphing}
\pgfplotsset{compat=1.13}
\usepackage{fp}
\usepgfplotslibrary{fillbetween}

\definecolor{bluelight}{RGB}{109,183,232}
\definecolor{bluemed}{RGB}{0,136,227}
\definecolor{purp}{RGB}{50, 69, 173}
\definecolor{blue}{RGB}{19,28,84}

% Defines "sgn" function
\def\sgn{\mathop{\rm sgn}\nolimits} 
% Defines the expectation function
\def\E{\mathop{\mathrm{E}}\nolimits} 

% Defines a function that ignores the zero on the y- or the x-axis, such that the origin
% is only labeled once. Activate in the axis options by adding the key: "ignore zero=x" or ""ignore zero=y."
\pgfplotsset{ignore zero/.style={%
		#1ticklabel={\ifdim\tick pt=0pt \else\pgfmathprintnumber{\tick}\fi}
	}}
	
%	
	
	\begin{document}
		
	
			\begin{tikzpicture}[>=stealth,element/.style={align=left},square/.style={regular polygon,regular polygon sides=4}]
			
%INPUTS: Use values from the file "preER_adv_seln.xls," which is generated by the program file "preER_adv_seln.do"
% >= 1 pre-period ER visits
	\newcommand\ATpreERone{0.452}	% always takers
	\newcommand\CpreERone{0.353}	% compliers
	\newcommand\NTpreERone{0.307}	% never takers
	\newcommand\mpreERone{-0.212}	% slope: line of best fit
	\newcommand\conspreERone{0.446}	% intercept: line of best fit
	
% >= 2 pre-period ER visits
	\newcommand\ATpreERtwo{0.258}	 % always takers
	\newcommand\CpreERtwo{0.187}	% compliers
	\newcommand\NTpreERtwo{0.147}	% never takers
	\newcommand\mpreERtwo{-0.164}	% slope: line of best fit
	\newcommand\conspreERtwo{0.256}	% intercept: line of best fit
	
% >= 3 pre-period ER visits
	\newcommand\ATpreERthree{0.171}	 % always takers
	\newcommand\CpreERthree{0.104}	% compliers
	\newcommand\NTpreERthree{0.085}	% never takers
	\newcommand\mpreERthree{-0.124}	% slope: line of best fit
	\newcommand\conspreERthree{0.164}	% intercept: line of best fit
	
% >= 4 pre-period ER visits
	\newcommand\ATpreERfour{0.114}	 % always takers
	\newcommand\CpreERfour{0.068}	% compliers
	\newcommand\NTpreERfour{0.051}	% never takers
	\newcommand\mpreERfour{-0.092}	% slope: line of best fit
	\newcommand\conspreERfour{0.110}	% intercept: line of best fit
	
	
	\newcommand\pC{0.15}	% P[D=0|Z=1]
	\newcommand\pI{0.41}	% P[D=1|Z=1]
			
			% Calculate the midpoint between 0 and pC
			\newcommand\xone{\pC*0.5}
			% Calculate the midpoint between pC and pI
			\newcommand\xtwo{\pC*0.5+\pI*0.5}
			% Calculate the midpoint between pI and 1
			\newcommand\xthree{\pI*0.5+0.5}
					
			% [yvalmax, yvalmin] Calculate Min/Max values of Y to set canvas size
			\newcommand\yvalmax{max(\conspreERone, \conspreERtwo, \conspreERthree, \conspreERfour)}
			\newcommand\yvalmin{0}
			
			% [yaxismin, yaxismax, yaxislength] Calculate graphing region for the y-axis
			\newcommand\yaxismin{\yvalmin*1.05}
			\newcommand\yaxismax{\yvalmax*1.1}
			\newcommand\yaxislength{\yaxismax-\yaxismin}
			
			% [yaxismin, yaxismax, yaxislength] Calculate graphing region for the y-axis
			\newcommand\pCdi{0.15}
			\newcommand\pIdi{0.41}
			
			% MTE(pC) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%			
			
			
			% Axis options
			\begin{axis}[					
			width=15cm,					% width of chart
			height=10cm,				% height of chart
			xlabel={$U_D$:  unobserved net cost of treatment},				% x-axis title
			ylabel={},					% y-axis title
			ylabel style={rotate=0},  % rotate the y-axis title
			samples=100,  				% sets the resolution for curved lines
			xmin=0,xmax=1,			    % x-axis range
			ymin=\yaxismin,
			%ymin=1.2*\Dinterceptr,ymax=1.05*\yaxismax,			% y-axis range
			ignore zero=y,			    % ignore the zero on the y-axis (defined in preamble)
			xtick={0,\pC,\pI,1},	    % only show these x ticks
			xticklabels={0,$p_C=\pCdi$, $p_I=\pIdi$,1}, % label x ticks
			ytick={0, \ATpreERone, \CpreERone, \ATpreERtwo, \CpreERtwo, \ATpreERthree, \CpreERthree-.007,\ATpreERfour, \CpreERfour},	    % only show these y ticks
			yticklabels={0,0.45,0.35,0.26,0.19,0.17,0.10,0.11,0.07},
			ymax = \yaxismax,
			legend style={draw=none},	% set legend style, specifically no outline
			legend cell align={left},	% align legend content to the left
			clip=false,					% place nodes outside of plot area
			xticklabel style={
									yshift=-5.5ex,
									name=label,
									append after command={(label.north) edge [solid] ++(0,6ex)},
									/pgf/number format/fixed,
									/pgf/number format/precision=2},
								x label style={at={(axis description cs:0.5,-0.17)},anchor=north}, % ncrease distance between title and x-axis
								yticklabel style={/pgf/number format/fixed},
								% place some extra y ticks ...
								extra y ticks={0, \NTpreERone, \NTpreERtwo, \NTpreERthree, \NTpreERfour},
								% ... but do not repeat the already existent labels
								% (from the "normal" ticks) ...
								extra y tick labels={},
								% ... and set the style of the extra ticks to `none'
								extra tick style={
									grid=none,
								},
			]						
			
			% preER1(p)
			\addplot[
				line width=1.8pt, bluelight, dashed, dash pattern=on 3.8pt off 2pt
			] coordinates {(0,\conspreERone) (1,\conspreERone+\mpreERone)};
			
						% preER2(p)
			\addplot[
				line width=1.8pt, bluemed, dashed, dash pattern=on 3.8pt off 2pt
			] coordinates {(0,\conspreERtwo) (1,\conspreERtwo+\mpreERtwo)};
			
			
						% preER3(p)
			\addplot[
				line width=1.8pt, purp, dashed, dash pattern=on 3.8pt off 2pt
			] coordinates {(0,\conspreERthree) (1,\conspreERthree+\mpreERthree)};
			
			
						% preER4(p)
			\addplot[
				line width=1.8pt, blue, dashed, dash pattern=on 3.8pt off 2pt
			] coordinates {(0,\conspreERfour) (1,\conspreERfour+\mpreERfour)};
			
			
			%Nodes for preER1(p)
			\node[outer sep=1.3pt,square, fill=bluelight,inner sep=2pt] (P) at (\xone, \ATpreERone) {};
			
			\node[outer sep=1.3pt,square, fill=bluelight,inner sep=2pt] (P) at (\xtwo, \CpreERone) {};

			\node[outer sep=1.3pt,square, fill=bluelight,inner sep=2pt] (P) at (\xthree, \NTpreERone) {};	
			
			%Nodes for preER2(p)
			\node[outer sep=1.3pt,square, fill=bluemed,inner sep=2pt] (P) at (\xone, \ATpreERtwo) {};
			
			\node[outer sep=1.3pt,square, fill=bluemed,inner sep=2pt] (P) at (\xtwo, \CpreERtwo) {};

			\node[outer sep=1.3pt,square, fill=bluemed,inner sep=2pt] (P) at (\xthree, \NTpreERtwo) {};	
			
			%Nodes for preER3(p)
			\node[outer sep=1.3pt,square, fill=purp,inner sep=2pt] (P) at (\xone, \ATpreERthree) {};
			
			\node[outer sep=1.3pt,square, fill=purp,inner sep=2pt] (P) at (\xtwo, \CpreERthree) {};

			\node[outer sep=1.3pt,square, fill=purp,inner sep=2pt] (P) at (\xthree, \NTpreERthree) {};	
			
			%Nodes for preER4(p)
			\node[outer sep=1.3pt,square, fill=blue,inner sep=2pt] (P) at (\xone, \ATpreERfour) {};
			
			\node[outer sep=1.3pt,square, fill=blue,inner sep=2pt] (P) at (\xtwo, \CpreERfour) {};

			\node[outer sep=1.3pt,square, fill=blue,inner sep=2pt] (P) at (\xthree, \NTpreERfour) {};		


			%Dashed Lines OR
			\addplot[
				line width=1pt, black, dashed, opacity=100, thin
			] coordinates {(0.15,0) (0.15,\conspreERone+0.15*\mpreERone)};
			
			\addplot[
				line width=1pt, black, dashed, opacity=100, thin
			] coordinates {(0.41,0) (0.41,\conspreERone+0.41*\mpreERone)};

			
			% Add right-hand yticklabel
			\node[anchor=west] () at (axis cs: 1,\NTpreERone) {$0.31$};
			\node[anchor=west] () at (axis cs: 1,\NTpreERtwo) {$0.15$};
			\node[anchor=west] () at (axis cs: 1,\NTpreERthree) {$0.08$};
			\node[anchor=west] () at (axis cs: 1,\NTpreERfour) {$0.05$};
			
	% add legend
	\legend{{\small line of best fit, $\geq$ 1 pre-period ER visit},{\small line of best fit, $\geq$ 2 pre-period ER visits},{\small line of best fit, $\geq$ 3 pre-period ER visits},{\small line of best fit, $\geq$ 4 pre-period ER visits},,,}
			
			% Put Always Takers, Compliers, and Never Takers on the x-axis
			\draw[draw=none,decorate,decoration={brace,amplitude=10pt,mirror,raise=4pt},yshift=0pt] (0,\yaxismin) -- (\pC, \yaxismin) node[pos=0.5, below, yshift = 0pt, align=left] {Always \\ Takers};
			\draw[draw=none,decorate,decoration={brace,amplitude=10pt,mirror,raise=4pt},yshift=0pt] (\pC,\yaxismin) -- (\pI, \yaxismin) node[pos=0.5, below, yshift = 0pt] {Compliers};
			\draw[draw=none,decorate,decoration={brace,amplitude=10pt,mirror,raise=4pt},yshift=0pt] (\pI,\yaxismin) -- (1, \yaxismin) node[pos=0.5, below, yshift = 0pt] {Never Takers};
			
			\end{axis}	
			
					
					\end{tikzpicture}
					
	\end{document}
